import random
from django.http import JsonResponse
from rest_framework.views import  APIView
from lxml import etree
from dal import models



class PieData(APIView):

    def get(self, request):
        data = {}
        res_list = []
        total = 0
        time = ""


        try:

            Remen = models.Remen.objects.order_by("id")
            New = models.New.objects.order_by("id")
            Jingdian = models.Jingdian.objects.order_by("id")
            DoubanTop = models.DoubanTop.objects.order_by("id")
            Comment = models.Comment.objects.order_by("id")


            dict_obj = {}
            dict_obj['value'] = Remen.count()
            dict_obj['name'] = "热门电影"
            res_list.append(dict_obj)
            dict_obj = {}
            dict_obj['value'] = New.count()
            dict_obj['name'] = "最新电影"
            res_list.append(dict_obj)
            dict_obj = {}
            dict_obj['value'] = Jingdian.count()
            dict_obj['name'] = "经典电影"
            res_list.append(dict_obj)
            dict_obj = {}
            dict_obj['value'] = DoubanTop.count()
            dict_obj['name'] = "高分电影"
            res_list.append(dict_obj)
            dict_obj = {}
            dict_obj['value'] = Comment.count()
            dict_obj['name'] = "最新影评"
            res_list.append(dict_obj)



            data['data'] = res_list
            data['total'] =  models.History.objects.order_by("id").count()
            data['time'] = models.History.objects.order_by("id").last().time.strftime('%Y-%m-%d')
            return JsonResponse(data)

        except Exception as e:
            print(e)
            data['code'] = 444
            return JsonResponse(data)



